package com.alex.space.commons.position;

import org.jooq.tools.json.JSONArray;
import org.jooq.tools.json.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Alex on 2017/8/25.
 */
public class GeoFormatConvertUtil {

    /**
     * Txt格式围栏转GeoJson
     */
    public static void Txt_2_GeoJson(String source, String target) {
        HashMap<String, String> rails = readRail(source);

        JSONObject featureCollection = new JSONObject();
        featureCollection.put("type", "FeatureCollection");
        JSONArray features = new JSONArray();
        featureCollection.put("features", features);
        int objectId = 1;

        for (Map.Entry entry : rails.entrySet()) {
            JSONObject feature = new JSONObject();
            feature.put("type", "Feature");

            JSONObject properties = new JSONObject();
            properties.put("OBJECTID", objectId);
            properties.put("name", entry.getKey());
            feature.put("properties", properties);

            JSONObject geometry = new JSONObject();
            geometry.put("type", "Polygon");
            JSONArray coordinates = new JSONArray();
            geometry.put("coordinates", coordinates);

            JSONArray polygon = new JSONArray();
            String[] points = entry.getValue().toString().split(",");
            for (String point : points) {
                JSONArray pt = new JSONArray();
                pt.add(Double.parseDouble(point.split(" ")[0]));
                pt.add(Double.parseDouble(point.split(" ")[1]));

                polygon.add(pt);
            }

            coordinates.add(polygon);

            feature.put("geometry", geometry);

            features.add(feature);
            objectId++;
        }

        save(target, featureCollection.toString());
    }

    private static void save(String path, String json) {
        try {

            FileWriter fw = new FileWriter(path);
            PrintWriter out = new PrintWriter(fw);
            out.write(json);
            out.println();
            fw.close();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static HashMap<String, String> readRail(String source) {
        HashMap<String, String> map = new HashMap<String, String>();
        try {
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(new FileInputStream(new File(source)), "UTF-8"));
            String lineTxt = null;
            StringBuffer finalLine = new StringBuffer();
            String tmpStr = "";
            int nameIdx = 1;
            int pxIdx = 2;
            int pyIdx = 3;
            while ((lineTxt = br.readLine()) != null) {
                String[] split = lineTxt.split(",");
                if (tmpStr.equals("")) {
                    tmpStr = split[nameIdx];
                    finalLine.append(split[pxIdx] + " " + split[pyIdx] + ",");
                } else {
                    if (split[nameIdx].equals(tmpStr)) {
                        finalLine.append(split[pxIdx] + " " + split[pyIdx] + ",");
                    } else {
                        map.put(tmpStr, finalLine.substring(0, finalLine.length() - 1));
                        tmpStr = "";
                        finalLine.setLength(0);
                        finalLine.append(split[pxIdx] + " " + split[pyIdx] + ",");
                    }
                }
            }
            map.put(tmpStr, finalLine.substring(0, finalLine.length() - 1));
            br.close();
        } catch (Exception e) {
            System.err.println("read errors :" + e);
        }
        return map;
    }

    public static void main(String[] args) {
        Txt_2_GeoJson("data/region_txt/1.txt", "data/region_json/1.json");
    }
}
